package util;

import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import model.Member;
import model.MovieSection;
import model.Purchase;
import model.PurchaseId;

public class PurchaseUtil {

    public void generalDoConfirm(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException, Exception {
        HttpSession session = request.getSession();
        String memberUsername = (String) session.getAttribute("memberUsername");
        MovieSection target = (MovieSection) session.getAttribute("targetMovieSection");
        String movieSectionId = target.getId();
        String[] arrSeats = (String[]) session.getAttribute("arrSeats");

        for (String seatNo : arrSeats) {
            PurchaseId newPurchaseId = new PurchaseId();
            newPurchaseId.setMovieSectionId(movieSectionId);
            newPurchaseId.setSeatNo(seatNo);
            Purchase newPurchase = new Purchase();
            newPurchase.setPurchaseId(newPurchaseId);
            newPurchase.setMemberUsername(memberUsername);
            newPurchase.setRemark("Unpaid");
            SeatHolder seatHolder = new SeatHolder(newPurchase);
            seatHolder.startHolding();
        }
    }

    public void generalDoPayment(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException, Exception {
        HttpSession session = request.getSession();
        String memberUsername = (String) session.getAttribute("memberUsername");
        MovieSection target = (MovieSection) session.getAttribute("targetMovieSection");
        String movieSectionId = target.getId();
        String[] arrSeats = (String[]) session.getAttribute("arrSeats");

        String paymentMethod = request.getParameter("paymentMethod");
        if (paymentMethod.equals("loyaltyPoint")) {
            // decrease the loyalty points
            Integer nLoyaltyPoint = (Integer) session.getAttribute("loyaltyPoint");
            Integer nTotalPayment = (Integer) session.getAttribute("totalPayment");
            if (nLoyaltyPoint < nTotalPayment) {
                throw new Exception("No enough loyalty points");
            }
            int remainedPoint = nLoyaltyPoint - nTotalPayment;
            Member updatedMember = new Member();
            updatedMember.setUserName(memberUsername);
            updatedMember.setLoyaltyPoint(remainedPoint);
            HibernateUtil.update(updatedMember);

            for (String seatNo : arrSeats) {
                SeatHolder seatHolder = SeatHolder.getSeatHolder(movieSectionId, seatNo);
                seatHolder.paidWithLoyalty();
            }
        } else {
            if (memberUsername.equals("nonmember") || memberUsername.equals("")) {
                //do nothing
            } else {
                //update loyalty points
                Integer nLoyaltyPoint = (Integer) session.getAttribute("loyaltyPoint");
                Integer nTotalPayment = (Integer) session.getAttribute("totalPayment");
                int newPoint = nLoyaltyPoint + nTotalPayment / 10;
                Member updatedMember = new Member();
                updatedMember.setUserName(memberUsername);
                updatedMember.setLoyaltyPoint(newPoint);
                HibernateUtil.update(updatedMember);
            }

            for (String seatNo : arrSeats) {
                SeatHolder seatHolder = SeatHolder.getSeatHolder(movieSectionId, seatNo);
                seatHolder.paidWithMoney();
            }
        }
    }

    private PurchaseUtil() {
    }
    private static PurchaseUtil instance;

    public static PurchaseUtil getInstance() {
        if (instance == null) {
            instance = new PurchaseUtil();
        }
        return instance;
    }
}
